package com.jiangyg.mall.core.utils.springframework;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 类描述：
 * 
 * @author jiangyg
 * @date 2020-07-08
 * @version 2.0
 */
public class ResourceUtils {

    /**
     * 功能描述：根据自动扫描路径获取Resource数组
     * 
     * @param locations 自动扫描路径
     * @return 返回匹配的扫描Resource
     */
    public static Resource[] obtainResourcesByPathMatching(List<String> locations) throws IOException {
        if (CollectionUtils.isNotEmpty(locations)) {
            List<Resource> resourceList = new ArrayList<>();
            PathMatchingResourcePatternResolver pmrpResolver = new PathMatchingResourcePatternResolver();
            for (String location : locations) {
                Resource[] resources = pmrpResolver.getResources(location);
                resourceList.addAll(Arrays.asList(resources));
            }
            return resourceList.toArray(new Resource[0]);
        }
        return new Resource[0];
    }

}
